﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Diagnostics;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

namespace OTPlus
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["access_token"] != null)
            {
                this.loadup(Session["access_token"].ToString());
            }

        }

        private void loadup(String token)
        {
            //this.repeaterThreads.DataSource = Core.Content.Activity.RetrieveAllThreadsViaToken(token);
            //this.repeaterThreads.DataBind();

            var threaddata = Core.Storage.ThreadSummaryCache.Instance.AccessCacheData();

            try
            { 
                threaddata.Sort((a, b) => DateTime.Compare(b.Published, a.Published));
            }
            catch (Exception exc)
            {
                this.lblError.Text = "Exception sorting :" + exc.ToString();
            }


            try
            {
                this.repeaterThreads.DataSource = threaddata;
                this.repeaterThreads.DataBind();
            }
            catch (Exception exc)
            {
                this.lblError.Text = "Exception binding :" + exc.ToString();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (Session["access_token"] != null)
            {
                this.repeaterThreads.DataSource = Core.Content.Activity.RetrieveSearchThreadsViaToken(Session["access_token"].ToString(), txtSearch.Text);
                this.repeaterThreads.DataBind();
            }
        }

    }
}